/*
 * =====================================================================================
 *
 *       Filename:  caculate_mask.c
 *
 *    Description:  i
 *
 *        Version:  1.0
 *        Created:  2019年09月19日 15时58分33秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


/*
 * return 0: not a mask
 *        >0: mask bits
 */
int caculate_mask_bits(const char *mask_str)
{
    in_addr_t addr = inet_addr(mask_str);
    //printf("0x%08X\n", addr);
    int i = 0;
    for(i = 0; i < 32; i++) {
        in_addr_t mask_addr = htonl(0xFFFFFFFF<<i);
        //printf("0x%08X: 0x%08X\n", 0xFFFFFFFF<<i, mask_addr);
        if(mask_addr == addr) {
            //printf("mask is %d\n", 32 - i);
            return 32 - i;
        }
    }
    return 0;
}


